package rickyGame.game.action
{
	import flash.geom.Point;
	
	import ricky.game.Actor;
	import ricky.game.action.Act2D;
	import ricky.game.graphic.Graphic;
	import ricky.quadtree.Leaf;
	import ricky.utils.FrameManager;
	import ricky.utils.TimeServer;
	import ricky.utils.Utils;
	
	import ricky.game.Actor2D;
	import rickyGame.game.GameState;
	import rickyGame.game.skill.SkillData;
	
	/**
	 * 跳跃行动
	 */
	public class JumpAct extends Act2D
	{
		public function JumpAct(actor:Actor,target:Point,skill:SkillData)
		{
			super(actor);
			this._target=target;
			var l:int=Utils.gridDistance1(actor.graphic.x,actor.graphic.y,_target.x,_target.y);

			if(l>skill.parameter1)//如果超出范围
			{
				var point:Point=_target.subtract(new Point(actor.graphic.x,actor.graphic.y));
				l=point.length;
				point.normalize(skill.parameter1);
				l=point.length;
				_target=new Point(actor.graphic.x+point.x,actor.graphic.y+point.y);
			}
				
			_coord=new Point();
			_skill=skill;
			_type="JumpAct";
		}
		
		override public function begin():void
		{
			_begin=TimeServer.now();
			_lastTime=_begin;
			FrameManager.inst.addLogic(getActor());
			//计算飞行角度
			var dx:Number=_target.x-this._actor.graphic.x;
			var dy:Number=_target.y-this._actor.graphic.y;
			var angle:Number=Math.atan2(dy,dx);//角度
			_vx=Math.cos(angle)*(Actor2D(_actor).speed+300);
			_vy=Math.sin(angle)*(Actor2D(_actor).speed+300);

		}
		
		
		override public function action(timeline:Number):void
		{
			var dot:Number=timeline-_lastTime;
			_lastTime=timeline;
			var faraway:Number=(timeline-_begin)/1000;
			if(faraway>10)
			{
				this.end();
				return;
			}
			
			var v1:Number=_vx*dot/1000;
			var v2:Number=_vy*dot/1000;
//			var x:Number=v1+this._actor.x;
//			var y:Number=v2+this._actor.y;
			
			var gx:int=Actor2D(_actor).gridX;
			var gy:int=Actor2D(_actor).gridY;
			
			this._actor.graphic.x=v1+this._actor.graphic.x;//Math.abs(targetX - x) >=5?:targetX;
			this._actor.graphic.y=v2+this._actor.graphic.y;//Math.abs(targetY - y) >=5?:targetY;
			//			
			var gx1:int=Actor2D(_actor).gridX;
			var gy1:int=Actor2D(_actor).gridY;
			
			if(this._actor.onMovingFun!=null)
				_actor.onMovingFun.call(null,_actor);
			
			if(gx!=gx1 || gy!=gy1)//格子位置改变
				onGridChange();
			
			if(isArrive())
				this.complete();
		}
		
		override public function end():void
		{

			Graphic(_actor.graphic).clothes.rotation=0;
			FrameManager.inst.removeLogic(getActor());
			super.end();
		}
		
		protected function onGridChange():void
		{
			Actor2D(_actor).lastMorton=Actor2D(_actor).morton;
			Actor2D(_actor).calcMorton();
			var cl:Leaf=GameState.inst.scene.tree.getLeaf(Actor2D(_actor).lastMorton)  ;//GameWorld.tree.getLeaf(myMor);
			if(cl)
				cl.remove(_actor);
			cl=GameState.inst.scene.tree.push(_actor,Actor2D(_actor).morton) as Leaf;
			
			if(_actor.stepFunction!=null)
				_actor.stepFunction.call(null,_actor);
		}
		
		/**
		 * 是否移动到目标x,y
		 */
		protected function isArrive():Boolean
		{

			_coord.x=this.actor.graphic.x;
			_coord.y=this.actor.graphic.y;
			
			if(_coord.subtract(_target).length<_radius)//if(x==this._currentObject.coordinate.x && this._currentObject.coordinate.y==y)
				return true;
			else
				return false;
		}
		//
		private var _coord:Point;//自身的坐标
		
		private var _radius:Number=20;//到达半径
		
		private var _force:Point;//力
		private var _gravity:Point;//重力
		
		private var _skill:SkillData;
		private var _lastTime:Number;//
		private var _begin:Number;
		
		private var _target:Point;//目标
		private var _vx:Number;//水平熟读
		private var _vy:Number;//垂直速度
	}
}